package shoppingCartFrame;

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import ShoppingCartJFrame.ShoppingCartUI;

import shoppingCart.ShoppingCart;
import shoppingCart.ShoppingCartItem;
import client.product.Tickets;

public class ItemPickerPanel extends JPanel implements Observer{


	ObjectList items;
	ShoppingCart cart;
	PanelRegistry registry;
	Tickets ticket = new Tickets();
	
	private JButton describeItem = new JButton("Describe Item");
	private JButton addItem = new JButton("Add Item");
	
	public ItemPickerPanel(StringReader reader){
		// Watch the applet registry to see when the Shopping Cart applet
				// becomes active
						registry = PanelRegistry.instance();
						registry.addObserver(this);

						items = new ObjectList();

				// Get the URL of the list of items that are for sale
						
							//fetchItems();
						

				// Put the items in the center of the screen 
						setLayout(new BorderLayout());
						add("Center", items);

						checkForShoppingCart();

				// Add this applet to the registry
						registry.addPanel("Item Picker", this);
						
						// Adds a list of products available (tickets) in a form of
						// StringReader and starts the frame
						
						fetchItems(reader);
						setBorder(BorderFactory.createTitledBorder("Product Information:"));
		
	}
	
	public void checkForShoppingCart()
	{
// See if the shopping cart has been loaded yet
		 JPanel panel = registry.findPanel("Shopping Cart");
		if (panel == null) return;

		ShoppingCartPanel cartPanel = (ShoppingCartPanel)
			panel;

// Get the shopping cart used by the shopping cart frame
		cart = cartPanel.getShoppingCart();

// Create the panel for adding items
		Panel southPanel = new Panel();

// Set up some command buttons for adding and describing items
		
		southPanel.add(describeItem);
			
		describeItem.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doDescribe();
			}
			});
		
		
		southPanel.add(addItem);
		addItem.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doAdd();
			}
			});
		
		add("South", southPanel);
	}
	
	public void update(Observable obs, Object ob)
	{
		if (cart != null) return;

		checkForShoppingCart();
	}
	
	// When someone presses the "Add Item" button, the doAdd method
		// is called.
			public void doAdd()
			{
		// Find out what object was selected
				Object ob = items.getSelectedObject();

				if (ob == null) return;

		// Add the item to the cart
				cart.addItem(((ShoppingCartItem)ob).copy());
			}
			
			// When someone presses "Describe Item", the doDescribe method
			// is called.
				public void doDescribe()
				{

			// Find out which object was selected
					Object ob = items.getSelectedObject();

					if (ob == null) return;

					ShoppingCartItem item = (ShoppingCartItem) ob;

			// MARCOS - TO BE DONE		
			// If it has a description URL, open it up in another frame
					if (item.itemDesc != null) {
						/*getAppletContext().showDocument(
							item.itemDesc, "descframe");*/
						System.out.println(item.itemDesc);
					}
				}
				
				// parseItem extracts an item name, cost, and URL from a string. The
				// items should be separated by |'s.
					public void parseItem(Tickets ticket)
					{
						
						for (int i = 0; i <ticket.getTicket().toArray().length; i++){
						int id = ticket.getTicket().get(i).getTicketID();
						String name = ticket.getTicket().get(i).getTkName();
						String description = ticket.getTicket().get(i).getTkDescription();
						BigDecimal cost = ticket.getTicket().get(i).getTkPrice();
						int qty = ticket.getTicket().get(i).getTkAmount();
						
						items.addObject(name,
								new ShoppingCartItem(id, name, description, cost, qty));
						}		
					}
					
					// fetchItems gets a list of available items from the web server and
					// uses parseItem to parse the individual items. If a line begins with
					// the # character, it is ignored (# is typically a comment character).

						public void fetchItems(StringReader reader)
						{
							//StringReader reader = new StringReader(client.getProdInfo().toString());
							
								  JAXBContext context;
								try {
									context = JAXBContext.newInstance(Tickets.class);
									System.out.println("Output from the XML");
									Unmarshaller um = context.createUnmarshaller();
									ticket = (Tickets) um.unmarshal(reader);
									
								} catch (JAXBException e) {
									System.out.println("ItemPickerApplet at the method fetchItems SHHHEEEAAAAT");
								}
								parseItem(ticket);
								
								/*	for (int i = 0; i <ticket.getTicket().toArray().length; i++){
										System.out.println("Ticket" + (i+1) +  " " +ticket.getTicket().get(i).getTkName()+
												" " + ticket.getTicket().get(i).getTkDescription() + "----" + 
												ticket.getTicket().get(i).getTkPrice());
										parseItem(ticket);
									}*/
									
						}
	
	
}
